package cn.web.web_module.controller;

import cn.itcast.pojo.admin;
import cn.itcast.pojo.user;
import cn.itcast.service.UserService;
import cn.itcast.util.ResponseEntity;
import cn.itcast.util.SmsDemo;
import cn.itcast.util.model.v20170525.SendSmsResponse;
import cn.message.service.adminService;
import cn.web.web_module.service.LogOrRegisterService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.message.service.adminService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 张松
 * @Description: 登入与注册功能
 * @Date: Created in 9:09 2020/7/11
 * @Modified By:
 */

@RestController
@RequestMapping("LogOrRegister")
public class LogOrRegisterController {

    @Resource
    private LogOrRegisterService logOrRegisterService;

    @Resource
    private UserService userService;

    @Resource
    private adminService adminService;


    /**
     * @Description: 用户登入后判断有没有手机号码
     * @auther: 张松
     * @date: 10:54 2020/7/11
     * @param: []
     * @return: cn.itcast.util.ResponseEntity
     */
    @RequestMapping("selectUserIfHavePhone")
    public ResponseEntity selectUserIfHavePhone(user user) {
        user oneUser = logOrRegisterService.selectUserIfHavePhone(user);
        try {
            if (oneUser == null) {
                return new ResponseEntity(oneUser);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity().error("该账户不存在");
    }


    /**
     *
     * @Description: 张松
     *
     * @auther: 账户手机号码绑定,用户输入接收的验证码正确就绑定手机号码
     * @date: 16:56 2020/7/13
     * @param: [user, code] 用户的实体类,必须输入openId,和需要被绑定的手机号码,code是用户接收的验证码
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @PostMapping("WXBindPhone")
    public ResponseEntity LogOrRegister(user user,String code) {
        Integer integer = logOrRegisterService.WXBindPhone(user, code);
        try {
            if (integer>0) return new ResponseEntity("绑定成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("您输入验证码不正确").error_300();
    }


    /**
     *
     * @Description:  查看用户是否有这个手机号
     *
     * @auther: 张松
     * @date: 9:20 2020/7/14
     * @param: [phone]
     * @return: cn.itcast.pojo.user
     *
     */
    @PostMapping("selectUserPhone")
    public ResponseEntity selectUserPhone(String phone) {
        user user = userService.selectUserPhone(phone);
        try{
            if (user!=null) return new ResponseEntity("此手机号存在");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("此手机号不存在").error_300();
    }

    /**
     *
     * @Description: 查看管理员表是否有此手机号
     *
     * @auther: 张松
     * @date: 10:47 2020/7/15
     * @param: [phone]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @PostMapping("selectAdminPhone")
    public ResponseEntity selectAdminPhone(String phone) {
        admin admin = logOrRegisterService.selectAdminPhone(phone);
        try{
            if (admin!=null) return new ResponseEntity("此手机号存在");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("此手机号不存在").error_300();
    }


    /**
     *
     * @Description: 管理员验证码登入
     *
     * @auther: 张松
     * @date: 10:45 2020/7/15
     * @param: [request, response, userphone, verification]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @PostMapping("userLog")
    public ResponseEntity userLog(HttpServletRequest request, HttpServletResponse response, String userphone , String verification){
        ResponseEntity responseEntity = logOrRegisterService.userLog(request, response, userphone, verification);
        try{
            if (responseEntity.getEntity()!=null) return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("登入错误,请重新登入").error_300();
    }


    @PostMapping("updatePassword")
    public ResponseEntity updatePassword(String userphone , String verification,String password){
        Integer integer = logOrRegisterService.updatePassword(userphone, verification, password);
        try{
            if (integer>0) return new ResponseEntity(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity("验证码错误,请重试").error_300();
    }


    /**
     *
     * @Description:
     *
     * @auther: 张松
     * @date: 18:24 2020/8/11
     * @param:
     * @return:
     *
     */

    @RequestMapping("getCodeDemo")
    public String getCodeDemo(){
        try{
            SendSmsResponse sendSmsResponse = SmsDemo.sendSms();
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "No";
    }
}
